package cn.com.taiji.core;

import javax.sql.DataSource;

import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.druid.DruidPlugin;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

/**
 * @author dongwei dongweia@mail.taiji.com.cn
 */
public abstract class DbInit {
    static Log log = LogFactory.get();

    public static DataSource getDataSourceDruid() {
        PropKit.use("db.setting");
        // 基本信息
        String url = PropKit.get("url");
        final String user = PropKit.get("username");
        final String password = PropKit.get("password");
        final String driver = PropKit.get("driver");

        DruidPlugin druidPlugin = new DruidPlugin(url, user, password);
        // remarks=true设置可以获取column remarks信息
        // useInformationSchema=true设置可以获取tables remarks信息
        druidPlugin.setDriverClass(driver);
        druidPlugin.setConnectionProperties("useInformationSchema=true;remarks=true");
        druidPlugin.start();

        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);

        arp.setShowSql(true);

        arp.setDialect(new com.jfinal.plugin.activerecord.dialect.OracleDialect());
        arp.setContainerFactory(new CaseInsensitiveContainerFactory());

        arp.setTransactionLevel(2);
        arp.start();
        return druidPlugin.getDataSource();
    }

}
